import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:jinke/utils/httputils/api.dart';
import 'package:jinke/utils/httputils/resultdata.dart';

import '../../../../../utils/util.dart';
import '../subs/pro_open_step1/controller.dart';
import '../subs/pro_open_step2/controller.dart';
import '../subs/pro_open_step3/controller.dart';
import 'index.dart';

class ProOpenController extends GetxController {
  ProOpenController();

  final state = ProOpenState();

  // tap
  void handleTap(int index) {
    Get.snackbar(
      "标题",
      "消息",
    );
  }

  void open() async {
    final step1Ctrl = Get.find<ProOpenStep1Controller>();
    final step2Ctrl = Get.find<ProOpenStep2Controller>();
    final step3Ctrl = Get.find<ProOpenStep3Controller>();
    var com = step1Ctrl.state.com;
    var factors = step2Ctrl.state.factors;
    var parm = step1Ctrl.state.params;
    var progresss = step3Ctrl.state.list;
    if (com == null) {
      return;
    }
    if (factors == null) {
      return;
    }
    if (progresss == null) {
      return;
    }
    if (parm == null) {
      return;
    }
    ResultData res =
        await ProListApi.open(com: com, factors: progresss, parm: parm);
    if (res.isSuccess) {
      EasyLoading.showSuccess('创建成功');
      Get.offNamedUntil(RouterPath.main, (route) => false);
    }
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.pageCtrl.addListener(() {
      var index = state.pageCtrl.page?.toInt();
      if (index != state.stepIndex) {
        state.stepIndex = index ?? 0;
        state.stepMan.setCurrentIndex(state.stepIndex);
        update();
      }
    });
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
